namespace OrderM
{
    public partial class Form1 : Form
    {
        public static OrderService service = new OrderService();
        public static Order order = new Order();
        public static int AddorEdit;

        public Form1()
        {
            InitializeComponent();

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //orderBindingSource2 = new BindingSource();
            orderBindingSource2.DataSource = service.orders;
            dataGridView1.DataSource = orderBindingSource2;
            //dataGridView1.DataSource = service.orders;

            this.comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)//AddOrder
        {
            Form2 form2 = new Form2();
            AddorEdit = 0;
            form2.ShowDialog();
            orderBindingSource2.ResetBindings(true);
            dataGridView1.DataSource = orderBindingSource2;
        }

        private void button2_Click(object sender, EventArgs e)//EditOrder
        {
            Form2 form2 = new Form2();
            int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["idDataGridViewTextBoxColumn"].Value);
            service.InqOrder(id);
            order = service.OrderInq[0];
            AddorEdit = 1;
            form2.textBox1.Text = Convert.ToString(order.Id);
            form2.textBox2.Text = Convert.ToString(order.customer);
            form2.textBox3.Text = Convert.ToString(order.StuffType);
            form2.textBox4.Text = Convert.ToString(order.price);
            form2.ShowDialog();

            //orderBindingSource2.ResetBindings(true);
            orderBindingSource2.DataSource = service.orders;
            dataGridView1.DataSource = orderBindingSource2;
        }

        private void button3_Click(object sender, EventArgs e)//SortByID
        {
            orderBindingSource2.DataSource = service.SortOrder(0);
            dataGridView1.DataSource = orderBindingSource2;
        }

        private void button9_Click(object sender, EventArgs e)//SortByPrice
        {
            dataGridView1.DataSource = service.SortOrder(1);
        }

        private void button4_Click(object sender, EventArgs e)//DeleteOrder
        {
            DialogResult RSS = MessageBox.Show(this, "Confirm to delete?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (RSS == DialogResult.Yes)
            {
                int i = dataGridView1.CurrentRow.Index;
                int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["idDataGridViewTextBoxColumn"].Value);
                DataGridViewRow row = dataGridView1.Rows[i];
                dataGridView1.Rows.Remove(row);
                service.DelOrder(id);
            }
            else
                return;
        }

        private void button5_Click(object sender, EventArgs e)//InquireOrder
        {
            string choice = comboBox1.SelectedItem.ToString();
            var input = textBox1.Text;
            bool flag = false;
            switch (choice)
            {
                case "ID":
                    {
                        flag = service.InqOrder(Convert.ToInt32(input));
                        if (!flag)
                            MessageBox.Show("Search failed.Order doesn't exist");
                        else
                        {
                            dataGridView1.DataSource = service.OrderInq;
                        }
                        break;
                    }
                case "customer":
                    {
                        flag = service.InqOrder(2, input);
                        if (!flag)
                            MessageBox.Show("Search failed.Order doesn't exist");
                        else
                        {
                            dataGridView1.DataSource = service.OrderInq;
                        }
                        break;
                    }
                case "commodityType":
                    {
                        flag = service.InqOrder(3, input);
                        if (!flag)
                            MessageBox.Show("Search failed.Order doesn't exist");
                        else
                        {
                            dataGridView1.DataSource = service.OrderInq;
                        }
                        break;
                    }
                case "price":
                    {
                        flag = service.InqOrder(Convert.ToDouble(input));
                        if (!flag)
                            MessageBox.Show("Search failed.Order doesn't exist");
                        else
                        {
                            dataGridView1.DataSource = service.OrderInq;
                        }
                        break;
                    }
                default: break;
            }
        }

        private void button6_Click(object sender, EventArgs e)//ExportOrder
        {
            bool flag = service.Export();
            if (flag)
                MessageBox.Show("Successfully export");
            else
                MessageBox.Show("Fail to export");
        }

        private void button7_Click(object sender, EventArgs e)//ImportOrder
        {
            bool flag = service.Import();
            if (flag)
            {
                MessageBox.Show("Successfully import");
                orderBindingSource2.ResetBindings(true);
                dataGridView1.DataSource = orderBindingSource2;
            }

            else
                MessageBox.Show("Fail to import");
        }

        private void button8_Click(object sender, EventArgs e)//Exit
        {
            this.Close();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void orderServiceBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void orderServiceBindingSource_CurrentChanged_1(object sender, EventArgs e)
        {

        }

        private void orderBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }


    }
}